-- godfather_bonus
-- create by shuzy
-- 道具

local UNIVERSE_GODFATHER_ID = 110;  -- 宇宙教父冈布奥编号

return {
    apply = function(source, target, classId, value)
        -- 宇宙教父强化零号基因专用
        local prop = PropM.combine(source, "slime_strengthen", UNIVERSE_GODFATHER_ID);
        local level = PropM.apply(prop, 0);
        local hp = source:queryAttrib("hp");
        local maxHp = source:queryAttrib("max_hp");

        if prop[3] > 0 then
            -- 触发特技发动事件
            EventMgr.fire(event.SPECIAL_SKILL_TRIGGER, { propId = prop[1] });
        end

        local function percentAttribBonus(source, attribName, force)
            local value = 0;
            if "hp" == attribName or "mp" == attribName then
                -- 千分比恢复属性
                local maxAttrib = "max_" .. attribName;
                value = math.modf(source:queryAttrib(maxAttrib) * force / 1000);
                -- 可增加上限
                attribName = "limit_" .. attribName;
            else
                -- 千分比增加属性
                -- 清空所有buff、debuff、special
                local baseAttrib = InterfaceM.calcBaseAttrib(source, attribName);

                value = math.modf(baseAttrib * force / 1000);
            end

            -- 最少加1点
            value = math.max(1, value);
            BonusM.doBonus({ 2, attribName, value, }, "apply_property");
        end

        local function hpToAttribBonus(source, attribName, force)
            local v = math.modf(force * (10 * (maxHp - hp) + maxHp) / maxHp / 10);
            v = math.max(1, v);
            BonusM.doBonus({ 2, attribName, v, }, "apply_property");
        end

        -- 奖励
        local arr;
        for i=1, level do
            arr = value[i];

            -- 只考虑单个奖励
            if arr[1] == 2 and  type(arr[2] == 'string')
                and string.startWith(arr[2], "per_") then
                -- 一次性的千分比属性奖励
                local force = arr[3];
                local attribName = string.sub(arr[2], string.len("per_") + 1);
                percentAttribBonus(source, attribName, force);
            elseif arr[1] == 2 and  type(arr[2] == 'string')
                and string.startWith(arr[2], "hp_to_") then
                -- 根据当前血量奖励（负相关）
                local force = arr[3];
                local attribName = string.sub(arr[2], string.len("hp_to_") + 1);
                hpToAttribBonus(source, attribName, force);
            else
                BonusM.doBonus(arr, "apply_property");
            end
        end
    end,
};